home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Extra.doc / Extra.doc.rsrc / TEXT_129.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  8.6 KB  |  204 lines

  1. Canvas
  2.     
  3.     Canvas is the object that you send messages to in order to draw.  You can only send these messages in response to the draw or pointer-req messages sent to a drawable view.
  4.  
  5.     The canvas represents the drawable surface of your drawable view.  It imposes a coordinate system upon the display.  Initially, the point 0,0 is the upper left hand corner and, the coordinates increase down and to the right.  The scaling is one unit to one pixel on the screen.  You can change the coordinate system to something more convenient, see set-unit-size below.
  6.  
  7.     [ This drawing system is not totally well-defined at the moment.  For example, under Macintosh, lines draw down and to the right, whereas on Windows they are centered.  Furthermore, there are some items that will most likely change in the future.  See the other bracketed comments. ]
  8.  
  9.  
  10.  
  11. Point Arguments
  12.     Many messages to canvas take one or more points as arguments.  You can specify these points in a number of ways, which ever is most convenient to your program.  Here we use the example of draw-rectangle which takes two points (the upper left and lower right corners).  Each of these is equivalent:
  13.  
  14.     canvas draw-rectangle 10, 20, 80, 90.
  15.         four coordinates
  16.  
  17.     p1 := new point of 10, 20.
  18.     p2 := new point of 80, 90.
  19.     canvas draw-rectangle p1, p2.
  20.         two points
  21.  
  22.     a := new array size 4.
  23.     a @ 1 := 10.
  24.     a @ 2 := 20.
  25.     a @ 3 := 80.
  26.     a @ 4 := 90.
  27.     canvas draw-rectangle a.
  28.         an array (or group) of coordinates
  29.  
  30.  
  31.     g := new group.
  32.     g add (new point of 10, 20).
  33.     g add (new point of 80, 90).
  34.     canvas draw-rectangle g.
  35.         a group (or array) or points
  36.  
  37.  
  38.     In the descriptions below, generally the coordinate form will be shown, although any form will work.  If the message takes other arguments, then the points always come last.
  39.  
  40.  
  41.  
  42. Basic Drawing
  43.     Most of these operations come in ‚Äòdraw-‚Äô and ‚Äòfill-‚Äô variants.  When drawing, the edge of the figure is stroked with a line using the current settings of line-thickness and paint.  When filling, the figure is solidly filled in with the paint.  Filling does not also draw the line on the edge.
  44.  
  45.  
  46. canvas draw-line x1, y1, x2, y2
  47.  draws a line between the two points
  48.  
  49.     Draws a line between the two points.  Drawing a line from point a to point b is the same as drawing it in the other order.
  50.  
  51.  
  52. canvas draw-rectangle x1, y1, x2, y2
  53.  draws a rectangle bounded by the two points
  54. canvas fill-rectangle x1, y1, x2, y2
  55.  fills a rectangle bounded by the two points
  56. canvas draw-ellipse x1, y1, x2, y2
  57.  draws an ellipse bounded by the two points
  58. canvas fill-ellipse x1, y1, x2, y2
  59.  fills an ellipse bounded by the two points
  60.  
  61.     These messages draw or fill the indicated graphic figure.  Ellipses are defined to fit within the given rectangle.
  62.  
  63.     
  64. canvas draw-window draws the display edge
  65. canvas fill-window fills the display
  66.  
  67.     These messages draw or fill entire view, including any borders that set-unit-size may have created.
  68.  [ Window is a poor choice of name for these messages. ]
  69.  
  70. ¬ª    Fill-window is an easy way to clear the view to a color other than white.
  71.  
  72.  
  73. canvas draw-polygon g    draws a polygon
  74. canvas fill-polygon g    fills a polygon
  75.  
  76.     These messages draw or fill a polygon.  The polygon is usually specified with a group or array of points or coordinates.  If there are four or fewer points, the points may be specified directly as arguments.  The polygon is always closed, even if the last point doesn‚Äôt equal the first.
  77.  
  78.  
  79. canvas draw-bezier p1, p2, p3, p4 segments n
  80.  draws a bezier curve from p1 to p4
  81.  
  82.     A bezier curve is specified with two endpoints, p1 & p4, and two control points, p2 & p3.  You can think of the curve starting out from p1, heading for p2, then curves to end up at p4 as if it was coming from p3.  The curve is approximated by line segments.  If you specify n, that number of segments is used: less is faster, more is more accurate.  If you leave n out, a suitable value will be chosen.
  83.  
  84.  
  85.  
  86. Drawing State
  87.     These two properties of canvas are used by the basic and text drawing messages.
  88.  
  89. canvas.paint        the current paint value
  90. canvas.paint := v    set the current paint value
  91.     
  92.     Paint is the gray-scale value to draw with.  It is a number between 0 (black) and 100 (white).  [ These numbers are probably backwards from what they should be.  ]
  93.  
  94. canvas.line-thickness        the current line width
  95. canvas.line-thickness := v    set the current line width
  96.     
  97.     This is the width of lines.  The value is interpreted according to the current coordinate grid.
  98.  
  99.  
  100.  
  101. Text Drawing
  102.  canvas draw-text t, x, y    draw the string t
  103.     
  104.     Draws the string t at the indicated point.  The point is the left end of the baseline that the text sits on.  The text is drawn in the current text size, font, style, as set by the set-font message and in the current color.
  105.  
  106.  
  107. canvas set-font size s    
  108.  set just the font size
  109. canvas set-font size s group i italic bold
  110.  set all of the font characteristics
  111.     
  112.     The size s is interpreted in the current units, initially (if you haven‚Äôt called set-unit-size) it is in pixels.  Group, if specified, sets the font as follows:
  113.     
  114.         canvas set-font group 1    Ô¨Å a Serif font
  115.         canvas set-font group 2    Ô¨Å a Sans-Serif font
  116.         canvas set-font group 3    Ô¨Å a Headline font
  117.     
  118.     You can specify bold and/or italic versions of the font.  You can specify just the size, if you want the other font settings to remain the same.
  119.  
  120.  
  121.  canvas measure-text t    the width of the string if drawn
  122.     
  123.     Returns the width of the string t if it were drawn.  The text is measured using the current text size, font, style, as set by the set-font message.  The width is returned in the current units.  Note that when measuring italic text, the top part of the text might lean further right than the result of the message indicates.
  124.  
  125.  
  126.  
  127. Units
  128.  
  129. canvas set-unit-size
  130.  reset the units to pixels
  131. canvas set-unit-size x, y    
  132.     scale the display to x, y units
  133. canvas set-unit-size x, y integer-scale
  134.  scale the display, keeping integer ratios
  135.     
  136.     These messages change the coordinate system used for drawing.  The first message form (no arguments) resets the coordinate system so that the point 0,0 is at the upper left of the view, and each unit is equal to one pixel, increasing down and to the right.
  137.  
  138.     The second and third message forms will create a coordinate system that has x units horizontally and y units vertically.  The units will be scaled so that this x by y grid takes up as much of the view as possible.  Note that unless the aspect ration of x to y is the same as the view‚Äôs, there will be border space around the grid.  For example, if the view is 100 x 150 pixels:
  139.  
  140.         
  141.         canvas set-unit-size 10, 10
  142.             Ô¨Å    ¬†            
  143.         
  144.         
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.         canvas set-unit-size 5, 2
  156.             Ô¨Å    ¬†            
  157.  
  158.     
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.     The scaling affects all future values passed in messages to canvas.  In particular, the line-thickness and size argument to set-font are interpreted in these new units.  However, the current settings are not affected.  For example, assuming the view is 150 by 100 pixels as above:
  169.  
  170.         canvas set-unit-size.        reset to pixel coordinates
  171.         canvas.line-thickness := 2.    draw 2 pixel wide lines
  172.         canvas set-unit-size 10, 10.        set new units
  173.         canvas draw-line 0, 5, 10, 5.        
  174.             draws a horizontal line, 2 pixels wide.
  175.         x := canvas.line-thickness.    Ô¨Å 0.2
  176.             returns scaled line-thickness
  177.         
  178.     The integer-scale flag causes the ratio between pixels and units to be an integer or the inverse of an integer.  This has the advantage that drawing maps to the screen pixels cleanly without round-off artifacts.  However, to achieve the integer ratio and still fit the coordinate grid on the screen, the grid may be considerably reduced.
  179.  
  180.  
  181. canvas pixels-per-unit return scale factor
  182.  
  183.     This message returns the number of pixels per unit of the coordinate grid.  If the coordinate grid is the default pixel grid, this is 1.  If you have changed the coordinate system with set-unit-size, then this is the scaling factor canvas is using to draw with.  In the two examples above, the results of this message would be:
  184.  
  185.         canvas pixels-per-unit Ô¨Å 10
  186.         canvas pixels-per-unit Ô¨Å 30
  187.  
  188.  
  189. canvas get-bounds     return the view bounds
  190.  
  191.     The get-bounds message returns to you a rectangle that this the full boundary of the view.  If the coordinate grid is the default pixel grid, then this is the size of the view in pixels.  If you have changed the coordinate system with set-unit-size, then this is the bounding rectangle in those units, which may be bigger than what you asked for.  In the two examples above, the results of this message would be:
  192.  
  193.         canvas get-bounds Ô¨Å (-2.5, 0) :: (12.5, 10)
  194.         canvas get-bounds Ô¨Å (0, -0.667) :: (5, 2.667)
  195.  
  196.  
  197.  
  198. Pointer
  199.  
  200. canvas.pointer        the location of the pointer
  201. canvas.pointer-active    true if the pointer is pressed
  202.  
  203.     These messages return information about the pointing device (a mouse, trackball, or pen).  The location returned is relative to the current units.
  204.